/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.loan;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import org.primefaces.event.DateSelectEvent;
import src.domain.customer.Customer;
import src.domain.loan.Loan;
import src.domain.loan.Loanprod;
import src.domain.loan.Loansimulate;
import src.services.customer.CustomerModel;
import src.services.loan.LoanprodModel;
import src.services.loan.LoansimulateModel;
import src.util.Messager;
import src.util.Utils;

/**
 *
 * @author jay
 */
@ManagedBean
@SessionScoped
public class LoansimulateBean implements Serializable {

    private List<SelectItem> avaibleProduct;
    private Loan loan;
    private List<Loansimulate> loansimulates;
    private LoanprodModel prodHelper;
    private boolean editMode = false;
    
    private String defaultButton="doNothingLoan";
    private String pricipalAmounts, interestAmounts, amounts;
    private boolean showLookup=false;
    
    
    public LoansimulateBean() {
        this.prodHelper = new LoanprodModel();
        this.loansimulates = new ArrayList<>();
        newData();
        editMode = false;
    }
        
    public boolean isEditMode() {
        return editMode;
    }
    
    public Loan getLoan() {
        return loan;
    }

    public void setLoan(Loan loan) {
        this.loan = loan;
    }

    public String getAmounts() {
        return amounts;
    }

    public String getInterestAmounts() {
        return interestAmounts;
    }

    public String getPricipalAmounts() {
        return pricipalAmounts;
    }
    
    public List<Loansimulate> getLoansimulates() {
        return loansimulates;
    }
        
    public List<SelectItem> getAvaibleProduct() {
        return avaibleProduct;
    }

    private void initForm() {
        this.avaibleProduct = prodHelper.listAll();
        this.editMode = true;
        this.showLookup=false;
    }
     
    public final void newData() {  
        initForm();
        if (this.loan!=null) loan = null;
        this.loan = new Loan();
    }
    
    public Customer getCustomerByCIF(String cif) {
        CustomerModel custHelper = new CustomerModel();
        Customer cust = custHelper.getByCif(cif);
        return cust;
    }
    
    public void getLoanCustomer(String cif) {
        boolean found=false;
        if ("null".equals(cif)) cif = loan.getCustomer().getCif();
        
        if (!cif.equals("") && cif!=null ) {
            Customer customer = getCustomerByCIF(cif);
            if (customer==null) {
                //reset
                this.loan.setCustomer(new Customer());
                Messager.showWarning("customerNotFound");
            }
            
            else {
                found=true;
                this.loan.setCustomer(customer);
            }
        }
        RequestContext.getCurrentInstance().addCallbackParam("success", found); 
    }
    
    public void getInterestRate() {  
        Integer prodId = loan.getLoanprod().getLoanprodId();
        BigDecimal amount = loan.getPrincipalAmount();
        
        if(prodId != null && amount != null) {
            BigDecimal intrate = prodHelper.getInterestRate(loan.getLoanprod().getLoanprodId(), loan.getPrincipalAmount());
            loan.setInterestRate(intrate); 
        } else loan.setInterestRate(null); 
    }  
    
    public void handleProdChange() {  
        
        Loanprod produk = new Loanprod();
        Integer id = loan.getLoanprod().getLoanprodId();
        
        if (id != null) {
            produk = prodHelper.getById(id);
            getInterestRate();
        }            
        loan.setLoanprod(produk);
    }  
    
    public String selectItemValueOf(List<SelectItem> avblList, Integer id) {
        return (String) Utils.selectItemValueOf(avblList, id);
    }
    
    public String getDefaultButton() {
        return defaultButton;
    }

    public void setDefaultButton(String defaultButton) {
        this.defaultButton = defaultButton;
    }

    public boolean isShowLookup() {
        return showLookup;
    }

    public void setShowLookup(boolean showLookup) {
        this.showLookup = showLookup;
    }
    
    public void handleCloseLookup(CloseEvent event) {
        this.showLookup=false;
    }
    
    public void handleDisbDateSelect(DateSelectEvent event) {  
        loan.setDisbursementDate(event.getDate());
        handleProdChange();
    }
    
    public void handleDueDateSelect(DateSelectEvent event) {  
        loan.setDueDate(event.getDate());
    }
    
     public void handlePeriod() {  
         if (loan.getPeriod() != null)
         loan.setDueDate(new LoansimulateModel().setMoth(loan.getDisbursementDate(), loan.getPeriod()));
    }
    
     public void simulasi() {
        this.loansimulates = new LoansimulateModel().simulation(loan);
        
        BigDecimal totalPricipalAmount = BigDecimal.ZERO;
        BigDecimal totalInterestAmount = BigDecimal.ZERO;
        BigDecimal totalAmount = BigDecimal.ZERO;
        
        for (int i=0; i<loansimulates.size(); i++) {
            Loansimulate loansim =  loansimulates.get(i);
            totalPricipalAmount = totalPricipalAmount.add(loansim.getAmount());
            totalInterestAmount = totalInterestAmount.add(loansim.getInterest());
            totalAmount = totalAmount.add(loansim.getTotal());
        }
        
        pricipalAmounts = Utils.formatNumber(totalPricipalAmount);
        interestAmounts = Utils.formatNumber(totalInterestAmount);
        amounts = Utils.formatNumber(totalAmount);
        
    }
     
     
     public void simulasi2() {
        //new LoansimulateModel().tess();
                
        
    }
     
     public void doPrint() {
         
        
       //  String jasperReport = "/reports/loan/loansim.jasper";
         //   JRDataSource datasource = new JRBeanCollectionDataSource(loansimulates, false);
           
           // JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, datasource);
            //JasperViewer.viewReport(jasperPrint);
     }
        
}
